package org.dromara.tearoom.service.impl;

import cn.hutool.core.bean.BeanUtil;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.tearoom.domain.TerRegionalInfo;
import org.springframework.stereotype.Service;
import org.dromara.tearoom.domain.bo.TerLockSerialnumBo;
import org.dromara.tearoom.domain.vo.TerLockSerialnumVo;
import org.dromara.tearoom.domain.TerLockSerialnum;
import org.dromara.tearoom.mapper.TerLockSerialnumMapper;
import org.dromara.tearoom.service.ITerLockSerialnumService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 门锁配置序号Service业务层处理
 *
 * @author xch
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class TerLockSerialnumServiceImpl implements ITerLockSerialnumService {

    private final TerLockSerialnumMapper baseMapper;

    /**
     * 查询门锁配置序号
     *
     * @param teroomId 主键
     * @return 门锁配置序号
     */
    @Override
    public TerLockSerialnumVo queryById(Long teroomId){
        return baseMapper.selectVoById(teroomId);
    }

    /**
     * 分页查询门锁配置序号列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 门锁配置序号分页列表
     */
    @Override
    public TableDataInfo<TerLockSerialnumVo> queryPageList(TerLockSerialnumBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TerLockSerialnum> lqw = buildQueryWrapper(bo);
        Page<TerLockSerialnumVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的门锁配置序号列表
     *
     * @param bo 查询条件
     * @return 门锁配置序号列表
     */
    @Override
    public List<TerLockSerialnumVo> queryList(TerLockSerialnumBo bo) {
        LambdaQueryWrapper<TerLockSerialnum> lqw = buildQueryWrapper(bo);
        lqw.like(StringUtils.isNotBlank(bo.getTeroomName()), TerLockSerialnum::getTeroomName, bo.getTeroomName());
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TerLockSerialnum> buildQueryWrapper(TerLockSerialnumBo bo) {
        LambdaQueryWrapper<TerLockSerialnum> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TerLockSerialnum::getTeroomId);
        lqw.eq(bo.getTeroomId()!=null,TerLockSerialnum::getTeroomId , bo.getTeroomId());
        lqw.like(StringUtils.isNotBlank(bo.getTeroomName()), TerLockSerialnum::getTeroomName, bo.getTeroomName());

        return lqw;
    }



    /**
     * 修改门锁配置序号
     *
     * @param bo 门锁配置序号
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TerLockSerialnumBo bo) {
        TerLockSerialnum update = BeanUtil.toBean(bo, TerLockSerialnum.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TerLockSerialnum entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除门锁配置序号信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
